package edu.scau.web.payment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.payment.domain.Payment;
import edu.scau.payment.service.PaymentService;
import edu.scau.payment.vo.PaymentVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Hidden
@RequestMapping("/paymentcrud/crud")
@Tag(name = "payment 基础数据操作接口")
public class PaymentCRUDController {
    @Autowired
    private PaymentService paymentService;


    @Operation(
            summary = "查找所有订单",
            description = "查找所有未删除的订单信息",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Payment.class)))
            }
    )
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<PaymentVo> paymentList = paymentService.listAll();
        return AjaxResult.success(paymentList);
    }

//    @Operation(
//            summary = "分页查找",
//            description = "分页查找所有未删除的信息",
//            parameters = {
//                    @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
//                    @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int"))
//            },
//            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
//                    description = "查询条件",
//                    required = false, // 查询条件可以为空
//                    content = @Content(
//                            mediaType = "application/json",
//                            schema = @Schema(implementation = PaymentVo.class)
//                    )
//            ),
//            responses = {
//                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Payment.class))), // 返回 PageInfo 对象
//                    @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) // 返回 AjaxResult 对象
//            }
//    )
//    @GetMapping("/listByPage")
//    public AjaxResult listByPage(
//            @RequestParam("pageNum") Integer pageNum,
//            @RequestParam("pageSize") Integer pageSize,
//            PaymentVo payment
//    ) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<PaymentVo> paymentList = paymentService.listByPage(payment);
//        PageInfo<Payment> pageInfo = new PageInfo<>(paymentList);
//        return AjaxResult.success(pageInfo);
//    }

    @Operation(
            summary = "根据ID查找",
            description = "根据ID查找信息",
            parameters = {
                    @Parameter(name = "paymentId", description = "ID", required = true, schema = @Schema(type = "Long"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Payment.class))),
                    @ApiResponse(responseCode = "204", description = "不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @GetMapping("/findPaymentById")
    public AjaxResult findPaymentById(@RequestParam("paymentId") String paymentId) {
        PaymentVo payment = paymentService.findPaymentByPaymentId(paymentId);
        if (payment != null) {
            return AjaxResult.success(payment);
        } else {
            return AjaxResult.success("不存在或者已删除！", HttpStatus.NO_CONTENT);
        }
    }

    @Operation(
            summary = "更新",
            description = "根据ID更新信息",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "更新条件,id是必须的",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = PaymentVo.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @PutMapping("/update")
    public AjaxResult updatePayment(@org.springframework.web.bind.annotation.RequestBody Payment payment) {
        try {
            int res = paymentService.updatePayment(payment);
            return res > 0 ? AjaxResult.success(HttpStatus.SUCCESS) : AjaxResult.error("更新失败");
        }catch (IllegalArgumentException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(
            summary = "插入订单",
            description = "插入订单信息",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "插入条件",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Payment.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @PutMapping("/insert")
    public AjaxResult insertPayment(@org.springframework.web.bind.annotation.RequestBody Payment payment) {
        try {
            int res = paymentService.insertPayment(payment);
            return res > 0 ? AjaxResult.success(200) : AjaxResult.error("插入失败！");
        }catch (IllegalArgumentException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(
            summary = "根据ID删除",
            description = "根据ID删除信息",
            parameters = {
                    @Parameter(name = "paymentId", description = "ID", required = true, schema = @Schema(type = "Long"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
                    @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
            }
    )
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("paymentId") String paymentId) {
        int res = paymentService.deletePaymentById(paymentId);
        return res > 0 ? AjaxResult.success("删除成功", 200) : AjaxResult.error("删除失败");
    }
}
